package cc.yihy.util.markdown;


import com.overzealous.remark.IgnoredHtmlElement;
import com.overzealous.remark.Options;
import com.overzealous.remark.Remark;
import com.overzealous.remark.convert.AbstractNodeHandler;
import com.overzealous.remark.convert.DocumentConverter;
import com.overzealous.remark.convert.NodeHandler;
import com.overzealous.remark.util.BlockWriter;
import org.jsoup.nodes.Element;

import static cc.yihy.util.markdown.MarkdownUtils.Image.*;


/**
 * @Package: cc.yihy.util
 * @date: 2017/11/21 15:14
 * @author: Yihy
 * @version: V1.0
 * @Description:
 */
public class MarkdownUtils {

    private final static Options GITHUB;
    private final static Remark REMARK;

    static {
        GITHUB = Options.github();
        GITHUB.ignoredHtmlElements.add(IgnoredHtmlElement.create("img", DATA_ORIGINAL_SRC, DATA_LAZY_SRC, DATA_SRC));
        GITHUB.inlineLinks = true;

        REMARK = new Remark(GITHUB);
        DocumentConverter converter = REMARK.getConverter();

        converter.addInlineNode(new Image(), "img");
    }


    public static String convertHtmlToMarkDown(String html) {
        if (html == null || html.length() == 0) {
            return html;
        }
        return REMARK.convert(html);
    }

    /**
     * Handles img tags.
     *
     * @author Phil DeJarnett
     */
    public static class Image extends AbstractNodeHandler {

        public static final String DATA_ORIGINAL_SRC = "data-original-src";
        public static final String DATA_SRC = "data-src";
        public static final String DATA_LAZY_SRC = "data-lazy-src";

        /**
         * Creates a link reference to an image, and renders the correct output.
         *
         * @param parent    The previous node walker, in case we just want to remove an element.
         * @param node      Node to handle
         * @param converter Parent converter for this object.
         */
        @Override
        public void handleNode(NodeHandler parent, Element node, DocumentConverter converter) {
            String url;

            if (node.hasAttr(DATA_ORIGINAL_SRC)) {
                url = converter.getCleaner().cleanUrl(node.attr(DATA_ORIGINAL_SRC));
            } else if (node.hasAttr(DATA_SRC)) {
                url = converter.getCleaner().cleanUrl(node.attr(DATA_SRC));
            } else if (node.hasAttr(DATA_LAZY_SRC)) {
                url = converter.getCleaner().cleanUrl(node.attr(DATA_LAZY_SRC));
            } else {
                url = converter.getCleaner().cleanUrl(node.attr("src"));
            }
            String alt = node.attr("alt");
            if (alt == null || alt.trim().length() == 0) {
                alt = node.attr("title");
                if (alt == null) {
                    alt = "";
                }
            }
            alt = converter.getCleaner().clean(alt.trim());
            if (converter.getOptions().inlineLinks) {
                if (alt.length() == 0) {
                    alt = "Image";
                }
                converter.getOutput().printf("![%s](%s)", alt, url);
            } else {
                String linkId = converter.addLink(url, alt, true);
                // give a usable description based on filename whenever possible
                if (alt.length() == 0) {
                    alt = linkId;
                }
                BlockWriter out = converter.getOutput();
                if (alt.equals(linkId)) {
                    out.printf("![%s][]", linkId);
                } else {
                    out.printf("![%s][%s]", alt, linkId);
                }
            }
        }
    }
}
